<!-- Bot Read Me -->
<!-- Author: Edwin -->
<template>
  <el-dialog style="width: 100%;height: 100%"
             :withHeader="false"
             :close-on-click-modal="false"
             :close-on-press-escape="false"
             width="951px"
             :visible.sync="visible"
             @close="canceldialog">
    <div class='bot-read-me'>
      <el-card shadow="never">
        <div class="bot-read-me-content">
          <el-form :model="dataForm"
                    ref="dataForm"
                    :inline="false"
                    @keyup.enter.native="dataFormSubmitHandle()"
                    label-width="120px"
                    v-loading="loading">
                <el-form-item :label="$t('custom.botStore.name')">
                  <el-input v-model="dataForm.name"></el-input>
                </el-form-item>
                <el-form-item :label="$t('custom.botStore.version')">
                  <el-input v-model="dataForm.version"></el-input>
                </el-form-item>
                <el-form-item  :label="$t('custom.botStore.description')">
                  <el-input type="textarea" :rows="2" v-model="dataForm.description"></el-input>
                </el-form-item>
          </el-form>
        </div>
      </el-card>
    </div>
    <template slot="footer">
      <el-button size="small"
                 @click="canceldialog">{{ $t('cancel') }}</el-button>
      <el-button size="small"
                 type="primary"
                 @click="dataFormSubmitHandle(false)">{{ $t('custom.botStore.save') }}</el-button>
    </template>
  </el-dialog>
</template>
<script>
import {createBot,updateBot,fetchBot} from '@/api/new'
export default {
  components: { },
  data () {
    return {
      visible: false,
      loading: false,
      dataForm: {
        id: '',
        applyTenant: false,
        createTime: '',
        description: '',
        enable: true,
        icon: '',
        level: '',
        name: '',
        subjectCode: '',
        updateTime: '',
        url: '',
        version: '1.0'
      }
    }
  },
  created () {
    this.dataForm.name = ''
    this.dataForm.version = ''
    this.dataForm.description = ''
  },
  computed: { },
  watch: { },
  methods: {
    init () {
      this.visible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        if (this.dataForm.id) {
          this.getInfo()
        }
      })
    },
    getInfo () {
      this.loading = true
      const params = {
        id: this.dataForm.id
      }
      fetchBot(params)
        .then(({ data: res }) => {
          if (res.code !== 0) {
            return this.$message.error(res.msg)
          }
          this.dataForm = {
            ...this.dataForm,
            ...res.data
          }
          if (this.dataForm.paramsJson === {}) {
            this.dataForm.paramsJson = this.dataForm.sysParamsJson
          }
          this.loading = false
        }).catch(() => { })
    },
    dataFormSubmitHandle (isVisible) {
      const params = {
        ...this.dataForm
      }
      const changeBotInfo = () => {
        if (!this.dataForm.id) {
          return createBot(params)
        } else {
          return updateBot(params)
        }
      }
      changeBotInfo().then(({ data: res }) => {
        if (res.code !== 0) {
          return this.$message.error(res.msg)
        }
        this.$emit('refreshDataList')
        this.visible = isVisible
      }).catch(() => { })
    },
    canceldialog () {
      this.visible = false
      this.$emit('refreshDataList')
    }
  }
}
</script>

<style scoped lang="scss">
@import "src/views/modules/mspbots/bots/my-bots/my-bot";
</style>
